﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Data;
namespace UIT_CV_Management
{
    public class LyLichSoLuoc
    {
        Database myDatabase = Database.shareDatabase;
        public String maLyLichSoLuoc;
        public String maNgheNghiep;
        public String maThongTinPhu;

        public String hoTen;
        public DateTime ngaySinh;
        public String phai;
        public String biDanh;
        public String noiSinh;

        //object
        public NgheNghiep ngheNghiep
        {
            get
            {
                return new NgheNghiep(maNgheNghiep);
            }
            set
            {
                ngheNghiep = value;
            }
        }
        public ThongTinPhu thongTinPhu
        {
            get
            {
                if (maThongTinPhu != "")
                    return new ThongTinPhu(maThongTinPhu);
                return null;
            }
            
            set
            {
                thongTinPhu = value;
            }
        }
        public LyLichSoLuoc(String _hoTen, DateTime _ngaySinh, String _noiSinh, String _phai, String _biDanh, ThongTinPhu _thongTinPhu, NgheNghiep _ngheNghiep)
        {
            maLyLichSoLuoc = this.getMaLyLichSoLuoc();

            hoTen = _hoTen;
            ngaySinh = _ngaySinh;
            phai = _phai;
            biDanh = _biDanh;
            noiSinh = _noiSinh;
            maNgheNghiep = _ngheNghiep.maNgheNghiep;
            if (_thongTinPhu != null)
                maThongTinPhu = _thongTinPhu.maThongTinPhu;
            else
                maThongTinPhu = "";

            //object
            
            ngheNghiep = _ngheNghiep;
            if (_thongTinPhu != null)
                thongTinPhu = _thongTinPhu;
            else
                thongTinPhu = null;

        }
        public LyLichSoLuoc(String _maLyLichSoLuoc)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM LyLichSoLuoc WHERE MaLyLichSoLuoc = '" + _maLyLichSoLuoc + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                maLyLichSoLuoc = table.Rows[0]["MaLyLichSoLuoc"].ToString();
                maNgheNghiep = table.Rows[0]["MaNgheNghiep"].ToString();
                maThongTinPhu = table.Rows[0]["MaThongTinPhu"].ToString();
                hoTen = table.Rows[0]["HoTen"].ToString();

                DataRow row = table.Rows[0];

                ngaySinh = row.Field<DateTime>("NgaySinh");
                phai = table.Rows[0]["Phai"].ToString();
                biDanh = table.Rows[0]["BiDanh"].ToString();
                noiSinh = table.Rows[0]["NoiSinh"].ToString();
            }
        }
        private String getMaLyLichSoLuoc()
        {
            String x;
            // from database
            x = Database.getID("LyLichSoLuoc", "MaLyLichSoLuoc");


            return x;
        }
        public void save()
        {
                String sqlLyLichSoLuoc = "INSERT INTO LyLichSoLuoc VALUES('" + maLyLichSoLuoc + "','" + hoTen + "','" + ngaySinh.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + noiSinh + "','" + phai + "','" + biDanh + "','" + ngheNghiep.maNgheNghiep + "','" + maThongTinPhu + "')";
                QueryResult result = myDatabase.Query(sqlLyLichSoLuoc);
                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi lưu dữ liệu vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else // save successfully
                {
                    ngheNghiep.save();
                    thongTinPhu.save();
                }
        }
        public void update()
        {
            bool isDupplicated = Database.isDupplicate("LyLichSoLuoc", "MaLyLichSoLuoc", maLyLichSoLuoc);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE LyLichSoLuoc SET HoTen = '" + hoTen + "', NgaySinh = '" + ngaySinh.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', NoiSinh = '" + noiSinh + "',Phai = '" + phai + "', BiDanh = '" + biDanh
                    + "' WHERE MaLyLichSoLuoc = '" + maLyLichSoLuoc + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi update liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {

                }

            }
        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM LyLichSoLuoc WHERE MaLyLichSoLuoc = '" + maLyLichSoLuoc + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {

                ngheNghiep.delete();
                thongTinPhu.delete();
            }
        }
    }
}
